package com.itheima.youdianda.controller;

import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.SysMenu;
import com.itheima.youdianda.service.SysMenuService;
import com.itheima.youdianda.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-02-12 14:27
 * @Desc 菜单Controller
 */
@Slf4j
@RestController
@RequestMapping("/sys_menu")
@Api(tags = "菜单管理接口")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 菜单修改
     * @Author: Pan Rundong
     * @Date: 2023-02-12 14:43
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param token
     * @param sysMenu
     * @return com.itheima.youdianda.common.R<java.lang.String>
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改菜单")
    public R<String> update(@RequestHeader("x-token") String token, @RequestBody SysMenu sysMenu) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        //如果成功，执行
        if (flag) {
            sysMenuService.updateById(sysMenu);
            return R.success("菜单修改成功");
        }
        return R.error("菜单修改失败");
    }

    /**
     * 删除菜单
     * @Author: Pan Rundong
     * @Date: 2023-02-12 17:41
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param token
     * @param id
     * @return com.itheima.youdianda.common.R<java.lang.String>
     */
    @GetMapping("/del")
    @ApiOperation(value = "删除菜单")
    public R<String> delete(@RequestHeader("x-token") String token, int id) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        //如果成功，执行
        if (flag) {
            sysMenuService.deleteMenu(id);
            return R.success("菜单删除成功");
        }
        return R.error("菜单删除失败");
    }

    /**
     * 获取所有菜单
     * @Author: Pan Rundong
     * @Date: 2023-02-12 17:44
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param token
     * @return com.itheima.youdianda.common.R<java.util.List < com.itheima.youdianda.domain.SysMenu>>
     */
    @GetMapping("/allData")
    @ApiOperation("获取所有菜单")
    public R<List<SysMenu>> getAll(@RequestHeader("x-token") String token) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            List<SysMenu> list = sysMenuService.getAllData();

            return R.success(list, "获取成功");
        }
        return R.error("获取菜单失败");
    }

    /**
     * 添加菜单
     * @Author: Pan Rundong
     * @Date: 2023-02-13 13:43
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param token
     * @param sysMenu
     * @return com.itheima.youdianda.common.R<java.lang.String>
     */
    @PostMapping("/add")
    @ApiOperation("添加菜单")
    public R<String> save(@RequestHeader("x-token") String token, @RequestBody SysMenu sysMenu) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        log.info(sysMenu.toString());
        if (flag) {
            sysMenuService.save(sysMenu);
            return R.success( "菜单添加成功");
        }
        return R.error("菜单添加失败");
    }

    @GetMapping("/rowInfo")
    @ApiOperation("根据Id获得菜单信息")
    public R<SysMenu> getById(@RequestHeader("x-token") String token, Integer id) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            SysMenu sysMenu = sysMenuService.getMenu(id);
            if (sysMenu == null) {
                return R.error("菜单信息为空");
            }
            return R.success( sysMenu,"查询到菜单");
        }
        return R.error("菜单查询失败");
    }
}
